<%@ page language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<%
    Connection conn = null;
    Class.forName("org.sqlite.JDBC");
    conn = DriverManager.getConnection("jdbc:sqlite://Users/Annie/Documents/workspace/HomeFinders/homefinders.db");
    ResultSet rsdoLogin = null;
    Statement psdoLogin=null;
    String sUserID=request.getParameter("sUserName");
    String sPassword=request.getParameter("sPwd");
    System.out.println("input "+sUserID + sPassword);
    String message= null;
    boolean loggedin = false;
    String sqlOption="SELECT * FROM login where username = '" +sUserID+"' and pw = '"+sPassword+"';";
    //System.out.println(sqlOption);
    psdoLogin = conn.createStatement();
    session = request.getSession();
    /*if (session.isNew()) {
        System.out.println ("new session");
    } else {
    	System.out.println ("old session");
    	System.out.println("username: "+session.getAttribute("username"));
    	System.out.println("pw: "+session.getAttribute("pw"));
    	
    }*/
    
    
    try{
    	rsdoLogin=psdoLogin.executeQuery(sqlOption);
    	System.out.println("after query");
    	if (rsdoLogin.next())
    	{
    		//System.out.println("Paid: "+rsdoLogin.getString("paid"));
    		if(rsdoLogin.getString("paid").equals("true"))
    		{
			   	  session.setAttribute("pw",sPassword);
			      session.setAttribute("username",sUserID);
			      message = "User logged in successfully";
			      response.sendRedirect("success.jsp?error="+message);
    		}
    		else
    		{
    			message="Payment unverified, please wait";
        	    response.sendRedirect("login.jsp?error="+message);
    		}	
			      
    	}
    	else 
    	{
    		message="No user or password matched";
    		//System.out.println(message);
    	    response.sendRedirect("login.jsp?error="+message);
    	}
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }  
    %>
    
    
   <%--  var value = "<%= loggedin %>";
    if(value)
    	{
    	if(typeof(Storage)!=="undefined")
    	  {
    	    sessionStorage.currentuser = "<%=request.getParameter("sUserName") %>";
    	    sessionStorage.loggedin = 1;
    	  }
    	}
    <% response.sendRedirect("success.jsp?error="+message); %>  %> --%>
    
    <%
    try{
         if(psdoLogin!=null){
             psdoLogin.close();
         }
         if(rsdoLogin!=null){
             rsdoLogin.close();
         }
         
         if(conn!=null){
          conn.close();
         }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }%>

<title>Do Login</title>
</head>
</body>
</html>